﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQLambda
{
    public static class IEnym_Ext
    {
        public static decimal SumElements<T>(this IEnumerable<T> ienum)
        {
            decimal sum = 0;

            foreach (var item in ienum)
            {
                sum += Convert.ToDecimal(item);
            }
            return sum;
        }

        public static decimal Product<T>(this IEnumerable<T> ienum)
        {
            decimal product = 1;

            foreach (var item in ienum)
            {
                product *= Convert.ToDecimal(item);
            }
            return product;
        }

        public static T MaxElement<T>(this IEnumerable<T> ienum)
            where T: IComparable<T>
        {
            T max = ienum.First();
            foreach (T item in ienum)
            {
                if (item.CompareTo(max) > 0)
                {
                    max = item;
                }
            }
            return max;
        }

        public static T MinElement<T>(this IEnumerable<T> ienum)
            where T : IComparable<T>
        {
            T min = ienum.First();
            foreach (T item in ienum)
            {
                if (item.CompareTo(min) < 0)
                {
                    min = item;
                }
            }
            return min;
        }

        public static decimal AverageSum<T>(this IEnumerable<T> ienum)
        {
            int count = 0;
            decimal sum = 0;
            foreach (var item in ienum)
            {
                sum += Convert.ToDecimal(item);
                count++;
            }
            return (sum / count);
        }
    }
}
